package com.zgc.common.aop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zgc.common.BaseContext;
import com.zgc.entity.EmpRole;
import com.zgc.entity.Permission;
import com.zgc.entity.RolePermission;
import com.zgc.exception.CustomException;
import com.zgc.service.EmpRoleService;
import com.zgc.service.PermissionService;
import com.zgc.service.RolePermissionService;
import com.zgc.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Component
@Aspect  //切面
public class CheckPermissionAop {
    @Autowired
    private EmpRoleService empRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;
    // 标识切⼊点
    @Pointcut("@annotation(com.zgc.common.aop.CheckPermission)")
    public void CheckPermission() {
    }
    // 前置通知
    @Before("CheckPermission()")
    public void before() {
        //查看要验证的方法是否有权限
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取请求对象方法
        HttpServletRequest request = attributes.getRequest();
        //获取请求对象方法 get post put
        String method = request.getMethod();
        //获取请求uri 请求地址
        String requestURI = request.getRequestURI();
        //获取当前用户
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<EmpRole> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(EmpRole::getEmpId, currentId.toString());
        EmpRole empRole=empRoleService.getOne(lambdaQueryWrapper);
        //严谨性判断
        if(null ==empRole){
            throw new CustomException("权限不足");
        }
        LambdaQueryWrapper<RolePermission> lw=new LambdaQueryWrapper();
        lw.eq(RolePermission::getRoleId, empRole.getRoleId());
        List<RolePermission> list=rolePermissionService.list(lw);
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            RolePermission rolePermission=list.get(i);
            Permission permission =permissionService.getById(rolePermission.getPermissionId());
            log.info("requestURI==="+requestURI.replace("http://localhost:8080","")+"=====permission.getRequestPath()==="+permission.getRequestPath());
            log.info("method=="+method+"==="+permission.getRequestMethod());
            if(requestURI.replace("http://localhost:8080","").equalsIgnoreCase(permission.getRequestPath())&&method.equalsIgnoreCase(permission.getRequestMethod())){
                flag = true;
                break;
            }
        }
        if (!flag) {
            log.info("当前⽤户权限不能调⽤:" + requestURI);
            throw new CustomException("权限不⾜");
        }
        log.info("通过权限校验:" + requestURI);
    }
}
